package com.iweb.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author iweb002 http://localhost:8090/hello
 * value:配置请求的路径
 * loadOnStartup:配置服务器启动的时候执行init(),值 0 - 9 之间
 */
@WebServlet(name = "hello",value = "/hello",loadOnStartup = 0,
    initParams = {
            @WebInitParam(name = "username",value = "jack"),
            @WebInitParam(name = "password",value = "123456")
    }
)
public class HelloServlet extends HttpServlet {

    // 第一次请求的时候执行
    // 如果配置了 loadOnStartup 则会在 服务器启动的时候执行
    @Override
    public void init() throws ServletException {
        System.out.println("init");
    }

    // ServletConfig : 获取服务器启动的时候传递的参数
    // 不会和 init() 同时执行
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("ServletName:"+config.getServletName());
        System.out.println("Servlet上下文:"+config.getServletContext());
        System.out.println("username:"+config.getInitParameter("username"));
        System.out.println("password:"+config.getInitParameter("password"));
    }

    // 业务逻辑方法 : 用户每次请求都会执行而且无论是GET POST DELETE PUT 优先级高于GET POST DELETE PUT
//    @Override
//    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("do service");
//        // 视图跳转
//        req.getRequestDispatcher("/main.html").forward(req,resp);
//    }

    // 用户每次请求都会执行 只处理 GET 请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("do Get");
        // 视图跳转
        req.getRequestDispatcher("/main.html").forward(req,resp);
    }
    // 用户每次请求都会执行 只处理 POST 请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("do Post");
        // 视图跳转
        req.getRequestDispatcher("/main.html").forward(req,resp);
    }
    // 用户每次请求都会执行 只处理 DELETE 请求
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("do Delete");
        // 只能返回数据,不能返回视图, HttpServletResponse做出相应
        PrintWriter writer = resp.getWriter();
        writer.println("hello delete");
        writer.flush();
        writer.close();
    }
    // 用户每次请求都会执行 只处理 PUT 请求
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("do Put");
        // 只能返回数据,不能返回视图
        PrintWriter writer = resp.getWriter();
        writer.println("hello put");
        writer.flush();
        writer.close();
    }

    // 服务器关闭的时候执行
    @Override
    public void destroy() {
        System.out.println("destroy");
        // 如果是单节点则没什么事情要做
        // 如果是分布式服务，会和其他的服务之间存在通信。在服务关闭的时候需要发出通知
    }
}


